import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:myapp/app/data/entities/movie_model.dart';
import 'package:myapp/app/theme/app_color.dart';

class MovieGridLayout extends StatelessWidget {
  final String title;
  final List<Movie> movies;
  final EdgeInsets? margin;
  final ValueChanged? onTap;

  const MovieGridLayout(this.title, this.movies, {this.margin, this.onTap});

  String _prefix(double origin, int index) {
    var originStr = origin.toString();
    var array = originStr.split(".");
    //todo 偷懒不做校验了
    return array[index];
  }

  Widget _richText(Movie item) {
    return RichText(
      text: TextSpan(
          text: "${_prefix(item.ranking, 0)}.",
          style: TextStyle(fontSize: 16, color: AppColor.orange),
          children: [
            TextSpan(
              text: _prefix(item.ranking, 1),
              style: TextStyle(fontSize: 12, color: AppColor.orange),
            ),
          ]),
    );
  }

  Widget _buildCardItem(Movie item, int index) {
    return Container(
      child: Stack(
        children: [
          ClipRRect(
            child: Image.network(
              item.img,
              fit: BoxFit.fitWidth,
            ),
            borderRadius: BorderRadius.circular(3),
          ),
          Visibility(
            child: Positioned(
              child: Container(
                child: Text(
                  item.onlyHave,
                  style: TextStyle(fontSize: 10, color: AppColor.white),
                ),
                padding: EdgeInsets.symmetric(vertical: 1, horizontal: 3),
                decoration: BoxDecoration(
                    color: AppColor.red80,
                    borderRadius: BorderRadius.circular(2)),
              ),
              top: 5,
              left: 5,
            ),
            visible: item.only,
          ),
          Positioned(
            child: _richText(item),
            bottom: 5,
            right: 5,
          )
        ],
      ),
    );
  }

  Widget _buildItem(Movie item, int index) {
    return InkWell(
      child: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildCardItem(item, index),
            Padding(
              padding: EdgeInsets.only(top: 10, bottom: 3),
              child: Text(
                item.title,
                style: TextStyle(fontSize: 14, color: AppColor.hex_333),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            Text(
              item.subTitle,
              style: TextStyle(fontSize: 12, color: AppColor.hex_666),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ],
        ),
      ),
      onTap: () => {onTap?.call(index)},
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          border:
              Border(bottom: BorderSide(width: 10, color: AppColor.hex_ddd))),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.only(bottom: 20),
            child: Text(
              title,
              style: TextStyle(fontSize: 18, color: AppColor.hex_333),
            ),
          ),
          GridView.count(
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            crossAxisCount: 2,
            crossAxisSpacing: 10,
            mainAxisSpacing: 10,
            children: movies
                .asMap()
                .keys
                .map((e) => _buildItem(movies[e], e))
                .toList(),
          ),
        ],
      ),
      padding: margin,
    );
  }
}
